package com.java.util;

import java.security.spec.AlgorithmParameterSpec;
import java.util.Base64;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import com.alibaba.fastjson2.JSON;

public class MyTools {

	public static String getUUID(){
		String uuid =  UUID.randomUUID().toString().replaceAll("-", "");
		return uuid;
	}
	
	public static boolean isEmpty(Object pObj) {
        if (pObj == null)
            return true;
        if (pObj == "" || pObj.equals("null"))
            return true;
        if (pObj instanceof String) {
            if (((String) pObj).length() == 0) {
                return true;
            }
        } else if (pObj instanceof Collection) {
            if (((Collection<?>) pObj).size() == 0) {
                return true;
            }
        } else if (pObj instanceof Map) {
            if (((Map<?, ?>) pObj).size() == 0) {
                return true;
            }
        }
        return false;
    }
	
	public static Set<Integer> randomNumbers(int min, int max, int count) {
        Random random = new Random();
        Set<Integer> uniqueNumbers = new HashSet<>();
        while (uniqueNumbers.size() < count) {
            int randomNumber = random.nextInt(max - min + 1) + min;
            uniqueNumbers.add(randomNumber);
        }
        return uniqueNumbers;
    }
	public static Map wxDecrypt(String encryptedData, String sessionKey, String vi) throws Exception {
        try {
        	byte[] encData = Base64.getDecoder().decode(encryptedData);
            byte[] iv = Base64.getDecoder().decode(vi);
            byte[] key = Base64.getDecoder().decode(sessionKey);
            AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
            String res = new String(cipher.doFinal(encData), "UTF-8");
            Map map = JSON.parseObject(res, Map.class);
    		return map;
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return null;
    }
	
	public static String getStringRandom(int length) {
        StringBuilder val = new StringBuilder();
        Random random = new Random();
        //参数length，表示生成几位随机数
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val.append((char) (random.nextInt(26) + temp));
            } else {
                val.append(random.nextInt(10));
            }
        }
        return val.toString();
    }
}
